package com.thirtydays.frame.util.ext

import android.content.ContentResolver
import android.content.ContentValues
import android.content.Intent
import android.net.Uri
import android.provider.MediaStore
import com.blankj.utilcode.util.EncodeUtils
import com.blankj.utilcode.util.FileIOUtils
import com.blankj.utilcode.util.TimeUtils
import com.thirtydays.frame.base.BaseApp
import java.io.File

/**
 * @author 小垚
 * @创建时间： 2021/1/20
 * @描述：
 **/
//添加视频到相册
fun File.writeToPhotoAlbum() {
    var localContentResolver: ContentResolver = BaseApp.sInstance.contentResolver
    var mimeType = "video/${name.substring(name.lastIndexOf(".") + 1, name.length)}"
    var paramLong = TimeUtils.getNowMills()
    val localContentValues = ContentValues()
    localContentValues.put("title", name)
    localContentValues.put("_display_name", name)
    localContentValues.put("mime_type", mimeType)
    localContentValues.put("datetaken", paramLong)
    localContentValues.put("date_modified", paramLong)
    localContentValues.put("date_added", paramLong)
    localContentValues.put("_data", absolutePath)
    localContentValues.put("_size", length())
    val localUri = localContentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, localContentValues)
    BaseApp.sInstance.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(this)))
}

/**
 * 图片文件转base64
 */
fun File.imageToBase64(): String {
    return "data:image/jpg;base64,${EncodeUtils.base64Encode2String(FileIOUtils.readFile2BytesByStream(this))}"
}